/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package miagemosaik.tools;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.*;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import miagemosaik.Photo;

/**
 *
 * @author Vincent
 */
public class FileUtils {

    private static File f;
    private static FileInputStream fis;
    private static FileOutputStream fos;
    private static ObjectInputStream ois;
    private static ObjectOutputStream oos;

    /**
     * ouvrir un fichier
     * @throws FileNotFoundException
     */
    public static File open(String path) throws FileNotFoundException {
        f = new File(path);
        if (f.exists()) {
            return f;
        } else {
            throw new FileNotFoundException();
        }
    }

    /**
     * initialise l'index
     * @param path : repertoire où sont stockés les images
     */
    public static int initIndex(String path) throws IOException {
        ArrayList<Photo> listPhoto = new ArrayList<Photo>();
        File repertoire = new File(path);
        open("file/Index.bin");
        String[] listeFichiers = repertoire.list();
        for (String liste : listeFichiers) {
            if (liste.endsWith(".jpg")) {
                try {
                    BufferedImage image = ImageIO.read(new File(path +"\\"+ liste));
                    Color moy = ImageUtils.moyRGB(image);
                    listPhoto.add(new Photo(liste, moy.getRed(), moy.getGreen(), moy.getBlue()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        writeList(listPhoto);
      //  afficherList(listPhoto);
        return listPhoto.size();
    }

    public static void afficherList(ArrayList<Photo> liste) {
        System.out.println(liste.size());
        for (Photo p : liste) {
            System.out.println(p);
        }
    }
    public static void writeList(ArrayList<Photo> list) {
        try {
            fos = new FileOutputStream(f);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(list);
            oos.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("liste mise à jour ! ");
    }

    public static Photo getSimilar(float r, float g, float b) {
        Photo pIn = new Photo(r, g, b);
        Photo pProche = new Photo(0, 0, 0);

        // 442 ? >> dist euclidienne max : racine(255² * 3)
        double distance = 442;
        double distanceTmp = 442;

        ArrayList<Photo> liste = null;
        try {
            fis = new FileInputStream(f);
            ois =
                    new ObjectInputStream(fis);
            liste =
                    (ArrayList<Photo>) ois.readObject();
            ois.close();
            fis.close();
        } catch (EOFException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

//init
        pProche.setName(liste.get(0).getName());
        pProche.setR(liste.get(0).getR());
        pProche.setG(liste.get(0).getG());
        pProche.setB(liste.get(0).getB());

        //search
        for (Photo pCurrent : liste) {
            distanceTmp = Math.sqrt(Math.pow(pCurrent.getR() - pIn.getR(), 2) +
                    Math.pow(pCurrent.getG() - pIn.getG(), 2) +
                    Math.pow(pCurrent.getB() - pIn.getB(), 2));
            if (distanceTmp < distance) {
                pProche.setName(pCurrent.getName());
                pProche.setR(pCurrent.getR());
                pProche.setG(pCurrent.getG());
                pProche.setB(pCurrent.getB());
                distance = distanceTmp;

            }
        }

        return pProche;
    }
}
